package cn.iocoder.yudao.module.trade.service.orderpostback;

import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.util.MyBatisUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.trade.controller.admin.orderpostback.vo.*;
import cn.iocoder.yudao.module.trade.dal.dataobject.orderpostback.OrderPostBackDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.trade.dal.mysql.orderpostback.OrderPostBackMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.trade.enums.ErrorCodeConstants.*;

/**
 * 订单回传 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class OrderPostBackServiceImpl implements OrderPostBackService {

    @Resource
    private OrderPostBackMapper orderPostBackMapper;

    @Override
    public Long createOrderPostBack(OrderPostBackSaveReqVO createReqVO) {
        // 插入
        OrderPostBackDO orderPostBack = BeanUtils.toBean(createReqVO, OrderPostBackDO.class);
        orderPostBackMapper.insert(orderPostBack);
        // 返回
        return orderPostBack.getId();
    }

    @Override
    public void updateOrderPostBack(OrderPostBackSaveReqVO updateReqVO) {
        // 校验存在
        validateOrderPostBackExists(updateReqVO.getId());
        // 更新
        OrderPostBackDO updateObj = BeanUtils.toBean(updateReqVO, OrderPostBackDO.class);
        orderPostBackMapper.updateById(updateObj);
    }

    @Override
    public void deleteOrderPostBack(Long id) {
        // 校验存在
        validateOrderPostBackExists(id);
        // 删除
        orderPostBackMapper.deleteById(id);
    }

    private void validateOrderPostBackExists(Long id) {
        if (orderPostBackMapper.selectById(id) == null) {
            throw exception(ORDER_POST_BACK_NOT_EXISTS);
        }
    }

    @Override
    public OrderPostBackDO getOrderPostBack(Long id) {
        return orderPostBackMapper.selectById(id);
    }

    @Override
    public PageResult<OrderPostBackDO> getOrderPostBackPage(OrderPostBackPageReqVO pageReqVO) {
        return orderPostBackMapper.selectPage(pageReqVO);
    }

    @Override
    public List<OrderPostBackDO> getByOrderId(Collection<Long> orderId) {
        LambdaQueryWrapperX<OrderPostBackDO> queryWrapperX = new LambdaQueryWrapperX<>();
        queryWrapperX.in(OrderPostBackDO::getOrderId, orderId);
        queryWrapperX.orderByDesc(OrderPostBackDO::getId);
        return orderPostBackMapper.selectList(queryWrapperX);
    }

    @Override
    public PageResult<DrainagePostBackRespVO> listByDrainageId(DrainagePostBackPageReqVO reqVO) {
        IPage<DrainagePostBackRespVO> page = orderPostBackMapper.selectOrderPostBackPageByDrainageId(
                MyBatisUtils.buildPage(reqVO),
                reqVO
        );

        return new PageResult<>(page.getRecords(), page.getTotal());
    }

}